package com.atguigu.day22.refltct05.r2;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class StudentTest {
    @Test
    public void test01(){
        String concat = Student.concat("安琪拉-", "米莱迪");
        System.out.println("concat = " + concat);

        Student s = new Student();
        s.show(30);
    }

    @Test
    public void test02(){
        //1.获取Class对象
        Class<Student> studentClass = Student.class;
        //2.获取所有的方法
        //获取本类以及父类中公共的方法
        //Method[] methods = studentClass.getMethods();
        //获取本类中所有的方法
        Method[] methods = studentClass.getDeclaredMethods();

        for (Method method : methods) {
            System.out.println("method = " + method);
        }
    }

    @Test
    public void test03() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //todo show
        //1.获取Class 对象
        Class<?> aClass = Class.forName("com.atguigu.reflect05.r2.Student");
        //2.获取指定的方法
        Method showMethod = aClass.getMethod("show", int.class);
        //3.创建对象
        Object o = aClass.newInstance();
        //4.调用方法
        showMethod.invoke(o,66);



    }
    @Test
    public void test04() throws Exception{
        //1.获取Class对象
        Class<Student> studentClass = Student.class;
        //2.获取指定的方法
        Method sumMethod = studentClass.getDeclaredMethod("sum", int.class, int.class);
        //3.禁止java语法检查
        sumMethod.setAccessible(true);
        //4.创建对象
        Student student = studentClass.newInstance();
        //5.执行方法 获取结果
        Object result = sumMethod.invoke(student, 22, 66);
        //6.输出结果
        System.out.println("result = " + result);

    }
    @Test
    public void test05() throws Exception{
     //todo divide
        //1.获取Class对象
        Class<Student> studentClass = Student.class;
        //2.获取指定的方法
        Method divideMethod = studentClass.getDeclaredMethod("divide", int.class, int.class);
        //3.进制语法检查
        divideMethod.setAccessible(true);
        //4.执行方法获取结果
        Object result = divideMethod.invoke(null, 10, 3);
        //5.输出结果
        System.out.println("result = " + result);


    }
}
